package com.maotouying.owlsmart.magnetPage;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.maotouying.owlsmart.R;
import com.maotouying.owlsmart.SmartUtil.NoScrollGridView;
import com.maotouying.owlsmart.SmartUtil.SystemUtil;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class magnetActivity extends AppCompatActivity {
    private ImageView iv_back;
    private magnetList_C magnetList_c;
    private List<magnetList_C> magnetList_cList = new ArrayList<magnetList_C>();
    private magnetList_A magnetList_a;
    private NoScrollGridView lv_magentlist;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_magnet);

        //顶部框颜色
        SystemUtil.setStatusBarColor(this, this.getResources().getColor(R.color.top_background_color));

        //拿到返回的参数
        Intent intent=getIntent();
        String homeSearch=intent.getStringExtra("search");

        //返回按钮
        iv_back = (ImageView)findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        lv_magentlist = (NoScrollGridView)findViewById(R.id.lv_magentlist);


        okhttp_text(homeSearch);

    }

    public void okhttp_text(String search) {

        magnetList_cList.clear();

        //创建okHttpclient的对象
        OkHttpClient okHttpClient = new OkHttpClient();
        //创建Request,Builder
        Request.Builder builder = new Request.Builder();
        //使用Request.Builder对象,调用Url方法,传入网路路径
        Request.Builder url = builder.url("https://btsow.cam/search/"+search);
        //使用Request.Builder对象,调用builder方法构件request对象
        Request request = url.build();
        //创建一个Call对象,参数就request对象
        Call call = okHttpClient.newCall(request);
        //使用call对象,调用enqueue方法,请求加入调度(异步加载)

        call.enqueue(new Callback() {
            //当请求失败的时候,调用此方法
            @Override
            public void onFailure(Call call, IOException e) {
                //andler.sendEmptyMessage(FALL);
            }

            //当请求成功的时候,调用此方法
            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String source = response.body().string();

                //解析和遍历文档，创建干净的解析
                Document list = Jsoup.parse(source);
                Elements listElement = list.getElementsByClass("data-list");

                //再遍历class="row"
                Document row = Jsoup.parse(listElement.html());
                Elements rowElement = row.getElementsByClass("row");

                Elements links = listElement.select("a[href]");
                for (Element idt : links){
                    System.out.println("href : "+ idt.attr("href"));//拿到href
                    System.out.println("title :"+ idt.attr("title"));//拿到标题
                    System.out.println("block :"+ idt.text());
                    magnetList_c =new magnetList_C();
                    //存储到实体类
                    magnetList_c.setHref(idt.attr("href").toString());
                    magnetList_c.setTitle(idt.attr("title").toString());
                    magnetList_c.setBlock(idt.text().toString().replace(idt.attr("title").toString(),""));
                    magnetList_cList.add(magnetList_c);
                }
                Message msg = new Message();
                msg.what = 16;
                Thandler.sendMessage(msg);//用activity中的handler发送消息

               // System.out.println(rowElement.html());
            }
        });

    }


    private Handler Thandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 16:
                    clickData((ArrayList<magnetList_C>) magnetList_cList);
                    break;


            }
        }
    };

    public void clickData(ArrayList<magnetList_C> magnetList_cList) {

        magnetList_a = new magnetList_A(R.layout.item_magnet, magnetActivity.this, magnetList_cList);


        lv_magentlist.setAdapter(magnetList_a);
        lv_magentlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                final TextView href = (TextView) view.findViewById(R.id.href);//链接
                final TextView title = (TextView) view.findViewById(R.id.title);//链接
                // 带参跳转
                Intent intent=new Intent();
                intent.putExtra("href", href.getText().toString());//设置参数,""
                intent.putExtra("title", title.getText().toString());//设置参数,""
                intent.setClass(magnetActivity.this, magnetDetailActivity.class);//从哪里跳到哪里
                magnetActivity.this.startActivity(intent);


            }
        });


    }





}